package com.ecactus.spinners;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class ArraySpinnerMainActivity extends AppCompatActivity {
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = findViewById(R.id.am_spinner);
        // 建立数据源 Android自带的样式simple_spinner_item
        String[] mItems = getResources().getStringArray(R.array.languages);
        // 建立Adapter并且绑定数据源
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItems);
        //Android自带的样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //自定义样式
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_zyd_item, mItems);
//        adapter.setDropDownViewResource(R.layout.simple_spinner_zyd_item);
        //绑定 Adapter到控件
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {

                String[] languages = getResources().getStringArray(R.array.languages);
                Toast.makeText(ArraySpinnerMainActivity.this, "你点击的是:" + languages[pos], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    public void stringSpinner(View view) {
        Intent intent = new Intent(this, ArrayStringSpinnerActivity.class);
        startActivity(intent);
    }

    public void nineSpinner(View view) {
        Intent intent = new Intent(this, ninePatchSpinnerActivity.class);
        startActivity(intent);
    }

    public void citySpinner(View view) {
        Intent intent = new Intent(this, CityMainActivity.class);
        startActivity(intent);
    }

}